import {ref} from 'vue';
import {SparePartListItem, GetSparePartListParam} from '../types';
import apiParts from '@/util/network_api/parts.js';

/**
 * 配件申请单的物料列表服务
 */
export default function () {
	const sparePartList = ref<Array<SparePartListItem>>([]);
	const total = ref(0);
	const getSparePartListParam = ref<GetSparePartListParam>({
		ascriptionType: '',
		brandId: '',
		coreMaterials: false,
		goodsIdList: [],
		goodsType: '',
		keyword: '',
		limit: 10,
		page: 1,
		source: '',
		typeId: '',
		typeThreeId: '',
		typeTwoId: '',
		unitId: '',
	});

	const getSparePartList = async (isAppend = true) => {
		try {
			const res = await apiParts.getSparePartList(getSparePartListParam.value);
			if (res.code === 20000) {
				if (isAppend) {
					sparePartList.value = [...sparePartList.value, ...res.data.rows];
				} else {
					sparePartList.value = res.data.rows;
				}
				total.value = Number(res.data.total);
				return '';
			}

			return res.message;
		} catch (e) {
			return '获取配件列表失败，请稍后重试';
		}
	};

	return {
		total,
		sparePartList,
		getSparePartListParam,
		getSparePartList,
	};
}
